package deployr.core;

import static java.text.MessageFormat.*;
import static java.util.Collections.*;

import java.util.*;

public class MavenIndex {

	public Map<String, String> indexPom = new TreeMap<String, String>();

	public Map<String, String> indexSha1 = new TreeMap<String, String>();

	public MavenIndex() {
		this(new TreeMap<String, String>(), new TreeMap<String, String>());
	}

	public MavenIndex(final Map<String, String> indexPom, final Map<String, String> indexSha1) {
		this.indexPom = indexPom;
		this.indexSha1 = indexSha1;
	}

	public void artifact(final String path, final String artifact) {
		indexPom.put(path, artifact);
	}

	public String getArtifact(final String sha1) {
		return indexPom.get(path(sha1));
	}

	public MavenIndex newIndex() {
		return new MavenIndex(unmodifiableMap(indexPom), unmodifiableMap(indexSha1));
	}

	public String path(final String checksum) {
		return indexSha1.get(checksum);
	}

	public void sha1(final String path, final String sha1) {
		indexSha1.put(sha1, path);
	}

	@Override
	public String toString() {
		final StringBuilder indexBuilder = new StringBuilder();
		for (final String sha1 : indexSha1.keySet()) {
			indexBuilder.append(format("{0}  http://repo2.maven.org/maven2/{1}.jar\n", sha1, indexSha1.get(sha1)));
		}
		return indexBuilder.toString();
	}

	public boolean isEmpty() {
		return indexPom.isEmpty() && indexSha1.isEmpty();
	}
}
